﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace AnOtherBlog.Core.Providers.SQL
{
	/// <summary>
	/// Implementa el acceso a datos para la clase Comment
	/// </summary>
	public partial class SQLDataProvider : DataProvider
	{
		/// <summary>
		/// Obtiene un comentario
		/// </summary>
		/// <param name="id">Identificador del comentario </param>
		/// <returns>Comment</returns>
		public override Comment CommentSelect(Guid id)
		{
			Comment comment = null;
			using (SqlConnection connection = DataConnection)
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand())
				{
					command.Connection = connection;

					command.CommandText = @"SELECT "
					+ @"PostCommentID, PostID, CommentDate, Author, Email, Website, CommentContent, Country, IP, IsApproved "
					+ @"FROM aob_PostComment WHERE PostCommentID=@PostCommentID";

					command.CommandType = CommandType.Text;
					command.Parameters.Clear();
					command.Parameters.Add(new SqlParameter("@PostCommentID", id));

					using (SqlDataReader reader = command.ExecuteReader())
					{
						if (reader.Read())
						{
							comment = SetCommentProperties(reader);
						}
					}
				}
			}
			return comment;
		}

		/// <summary>
		/// Inserta un comentario
		/// </summary>
		/// <param name="comment">Comentario a insertar</param>
		public override void CommentInsert(Comment comment)
		{
			throw new NotImplementedException();
		}

		/// <summary>
		/// Modifica un comentario
		/// </summary>
		/// <param name="comment">Comentario a modificar</param>
		public override void CommentUpdate(Comment comment)
		{
			throw new NotImplementedException();
		}

		/// <summary>
		/// Suprime un comentario
		/// </summary>
		/// <param name="comment">comentario a suprimir</param>
		public override void CommentDelete(Comment comment)
		{
			throw new NotImplementedException();
		}



		#region Utilities Methods

		/// <summary>
		/// Fija todas las propiedades de un comentario
		/// </summary>
		/// <param name="reader">El <see cref="System.Data.SqlClient.SqlDataReader"/>que contiene los datos</param>
		/// <returns>Un nuevo <see cref="AnOtherBlog.Core.Comment"/> comentario</returns>
		private Comment SetCommentProperties(SqlDataReader reader)
		{
			Comment comment = new Comment();

			if (!reader.IsDBNull(0)) comment.PostCommentID = reader.GetGuid(0);
			if (!reader.IsDBNull(1)) comment.PostID = reader.GetGuid(1);
			if (!reader.IsDBNull(2)) comment.CommentDate = reader.GetDateTime(2);
			if (!reader.IsDBNull(3)) comment.Author = reader.GetString(3);
			if (!reader.IsDBNull(4)) comment.Email = reader.GetString(4);
			if (!reader.IsDBNull(5)) comment.Website = reader.GetString(5);
			if (!reader.IsDBNull(6)) comment.CommentContent = reader.GetString(6);
			if (!reader.IsDBNull(7)) comment.Country = reader.GetString(7);
			if (!reader.IsDBNull(8)) comment.IP = reader.GetString(8);
			if (!reader.IsDBNull(9)) comment.IsApproved = reader.GetBoolean(9);

			comment.IsNew = false;
			comment.IsProxy = false;

			return comment;
		}

		#endregion

	}
}
